package cn.shadow.controller;

import cn.shadow.service.MiddleService;
import cn.shadow.service.MyException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Controller;
import org.springframework.util.concurrent.ListenableFuture;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;

@Controller
@ResponseBody
public class AccountController {

    @Autowired
    MiddleService middleService;

    @GetMapping("/transfer")
    public String transfer() throws MyException {
        middleService.callTransfer();
        return "success";
    }
    @Autowired
    ExecutorService ex;

    @GetMapping("/async")
    @Async
    public Future <String> async()  {

        ex.submit(()->{
            Thread.sleep(1000);
            return new AsyncResult<>("hello1");
        });
        System.out.println("hello");
        return new AsyncResult<>("hello2");
    }

}
